﻿using MediatR;
using System.Reflection.Metadata;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.Domains.ScratchTheFat;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.DTOs.ReportDTOs;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.SplitDetails
{
    public class SplitDetailsHander : IRequestHandler<SplitDetailsCommand, Resqults<SplitDetailtDTO>>
    {
        private readonly IBateRepository<SplitRU> splitRu;
        private readonly IBateRepository<Stash> stash;
        private readonly IBateRepository<Shopping> shop;
        private readonly IBateRepository<Scale> scale;

        public SplitDetailsHander(IBateRepository<SplitRU> splitRu, IBateRepository<Stash> stash, IBateRepository<Shopping> shop, IBateRepository<Scale> scale)
        {
            this.splitRu = splitRu;
            this.stash = stash;
            this.shop = shop;
            this.scale = scale;
        }

        public async Task<Resqults<SplitDetailtDTO>> Handle(SplitDetailsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults<SplitDetailtDTO> res = new Resqults<SplitDetailtDTO>();
                var list = from a in splitRu.GetAll()
                           join b in stash.GetAll() on a.RepositoryID equals b.Stashid
                           join c in shop.GetAll() on a.ShoppIngId equals c.ShoppingDesignationid
                           join d in scale.GetAll() on a.ScaleId equals d.Scaleid
                           select new SplitDetailtDTO()
                           {
                               DocumentsDate=a.DocumentsDate,
                               DocumentNo=a.DocumentNo,
                               ScaleId=d.ScaleName,
                               RepositoryID=b.StashName,
                               ShoppIngId=c.ShoppingDesignationName,
                               WeightNum=a.WeightNum,
                               GrossWeight = a.GrossWeight,
                               Tare = a.Tare,
                               NetWeight = a.NetWeight
                           };
                if (request.BeginTime != null)
                {
                    list = list.Where(x => x.DocumentsDate >= request.BeginTime);
                }
                if (request.EndTime != null)
                {
                    list = list.Where(x => x.DocumentsDate <= request.EndTime);
                }
                res.Date = list.ToList();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
